package com.mall.service.marketing.controller;

import com.mall.autoconfig.interceptor.BaseContext;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.SmsFlashPromotion;
import com.mall.model.vo.PageResult;
import com.mall.service.marketing.aop.MallLog;
import com.mall.service.marketing.service.SmsFlashPromotionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/flash")
public class SmsFlashPromotionController {

    @Autowired
    private SmsFlashPromotionService smsFlashPromotionService;

    /**
     * 根据活动名称分页查询
     * @param keyword
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "flashCache" ,key = "'flashInfo'", unless = "#result.data==null")
    public CommonResult list(@RequestParam(value = "keyword", required = false) String keyword,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        PageResult pr = smsFlashPromotionService.list(keyword, pageNum, pageSize);
        return CommonResult.success(pr);
    }

    /**
     * 添加活动
     * @param smsFlashPromotion
     * @return
     */
    @PostMapping("/create")
    @CacheEvict(value = "flashCache", allEntries = true)
    public CommonResult create(@RequestBody SmsFlashPromotion smsFlashPromotion) {
        smsFlashPromotion.setCreateTime(new Date());
        boolean save = smsFlashPromotionService.save(smsFlashPromotion);
        if (save) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.success("添加失败");
    }

    /**
     * 删除活动
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    @CacheEvict(value = "flashCache", allEntries = true)
    public CommonResult delete(@PathVariable Integer id){
        boolean removeById = smsFlashPromotionService.removeById(id);
        if (removeById) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.success("删除失败");
    }

    /**
     * 编辑活动
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    @CacheEvict(value = "flashCache", allEntries = true)
    public CommonResult update(@PathVariable Integer id, @RequestBody SmsFlashPromotion smsFlashPromotion){
        boolean updateById = smsFlashPromotionService.updateById(smsFlashPromotion);
        if (updateById) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.success("修改失败");
    }

    /**
     * 修改上下线状态
     * @param id
     * @param status
     * @return
     */
    @MallLog(timeout = 50)
    @PostMapping("/update/status/{id}")
    @CacheEvict(value = "flashCache", allEntries = true)
    public CommonResult updateStatus(@PathVariable Integer id, Integer status){
        SmsFlashPromotion smsFlashPromotion = smsFlashPromotionService.getById(id);
        smsFlashPromotion.setStatus(status);
        boolean updateById = smsFlashPromotionService.updateById(smsFlashPromotion);
        if (updateById) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.success("修改失败");
    }

    /**
     * 获取活动详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult findById(@PathVariable Integer id){
        SmsFlashPromotion smsFlashPromotion = smsFlashPromotionService.getById(id);
        return CommonResult.success(smsFlashPromotion);
    }
}
